package com.wlc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlc.mapper.MenuMapper;
import com.wlc.service.TestService;
import com.wlc.vo.Menu;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王立朝
 * @date 2023/3/30
 * @description:
 */
@Service
public class TestServiceImpl implements TestService {

    @Resource
    private MenuMapper menuMapper;
    @Override
    public List<Menu> findMenu() {
        //数据查询
        List<Menu> list = menuMapper.selectList(new QueryWrapper());
        //新建一个用于接收数据的list
        List<Menu> resultList = new ArrayList<>();
        for (Menu result : list) {
            if (result.getParentId() == 0) {
                //调用方法给子类添加数据
                resultList.add(getMenuTree(result, list));
            }
        }
        return resultList;
    }
    private Menu getMenuTree(Menu result, List<Menu> list) {
        for (Menu menu : list) {
            //如果父类主键等于传过来实体类的ID
            if (menu.getParentId() == result.getID()) {
                if (result.getChildMenu() == null) {
                    result.setChildMenu(new ArrayList<>());
                }
                // 递归调用
                result.getChildMenu().add(getMenuTree(menu, list));
            }
        }
        return result;
    }

}
